
import GameManager from "./GameManager";

export enum MouseState{
    MouseDown,
    MouseUp,
    MouseMove,
}

export default class InputManager {

    private static m_instance:InputManager;
    public static GetInstance():InputManager
    {
        if(this.m_instance==null)
        {
            this.m_instance=new InputManager();
        }
        return this.m_instance;
    }



    constructor()
    {
        this.AddMouseEvent();
    }


    public GetScreenPoint(){
        var point: Laya.Vector2 = new Laya.Vector2();
        point.x = Laya.MouseManager.instance.mouseX;
        point.y = Laya.MouseManager.instance.mouseY;

        return point;
    }
 
    private m_showCtrTip=true;
    
	private distance:number = 0.0;
    private m_mouseDowned:boolean=false;
    private first:boolean=true;

    private m_moveDir:Laya.Vector2=new Laya.Vector2();

    private m_camera:Laya.Transform3D;

    private round:Laya.Sprite; //控制圆

    private direction:Laya.Sprite; //方向圆

    private speed:number = 0;
    private angle:number;
    private centerX:number = -1;
    private centerY:number = -1;

    //PC
    AddMouseEvent()
    {
        //鼠标事件监听
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.mouseDown);
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.mouseUp);

        if (this.m_showCtrTip) {
            this.round = new Laya.Sprite;
            this.direction = new Laya.Sprite;
            Laya.stage.addChild(this.round);
            Laya.stage.addChild(this.direction);
        }
    }
    

    public GetMoveDir()
    {
        this.m_moveDir.x=0;
        this.m_moveDir.y=0;
        if(this.speed>0)
        {
            this.m_moveDir.x = Laya.stage.mouseX - this.centerX;
            this.m_moveDir.y = Laya.stage.mouseY - this.centerY;
            this.angle = Math.atan2(this.m_moveDir.y,this.m_moveDir.x);
        }
        return  this.m_moveDir;
    }


  
    mouseDown()
    { 
        if (this.m_showCtrTip) {
            this.direction.pos(Laya.stage.mouseX,Laya.stage.mouseY);
            this.direction.graphics.drawCircle(0, 0, 5, "#0000ff");
            this.round.graphics.drawCircle(Laya.stage.mouseX, Laya.stage.mouseY, 25, "#00ffff");
            this.round.graphics.drawCircle(Laya.stage.mouseX, Laya.stage.mouseY, 3, "#ffffff");
        }
        this.centerX = Laya.stage.mouseX;
        this.centerY = Laya.stage.mouseY;
        Laya.stage.on(Laya.Event.MOUSE_MOVE,this,this.mouseMove);

    }

    mouseUp()
    {
        Laya.stage.off(Laya.Event.MOUSE_MOVE,this,this.mouseMove);
        this.speed = 0;
        if (this.m_showCtrTip) {
            this.round.graphics.clear();
            this.direction.graphics.clear(); 
        }

    }


    mouseMove(e:MouseEvent)
    {
        if(this.centerX>=0&&this.centerY>=0)
        {
            //计算两点距离 如果超过一定距离 就停留在距离最大值处
            let dis = this.dis(this.centerX,this.centerY,Laya.stage.mouseX,Laya.stage.mouseY);
            if (this.m_showCtrTip) 
            {
                if(dis>20){
                    this.direction.pos(this.centerX + Math.cos(this.angle)*20,this.centerY + Math.sin(this.angle)*20);
                }else{
                    this.direction.pos(Laya.stage.mouseX,Laya.stage.mouseY);
                }
            }
           
            //如果距离太小 就代表没动 
            if(dis>1){
                this.speed = 3;  //此处还可以通过距离 控制速度
            }else{
                this.speed = 0;
            }
        }
    }


    dis(centerX,centerY,mouseX,mouseY)
    {
        let dx:number = centerX - mouseX;
        let dy:number = centerY - mouseY;
        let distance:number = Math.sqrt(dx*dx+dy*dy);
        return distance;
    }



}